<template>
	<view class="box">
		<uni-list v-for="item,index in message">
			<uni-list-item @click="detailsMessage(item.id)" :title="item.title" clickable
				:rightText="item.date + ' ' + item.name"></uni-list-item>
		</uni-list>

		<view class="btn">
			<button @click="publishMessage" type="primary">去留言</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				message: []
			}
		},
		methods: {
			// 留言详情：
			detailsMessage(itemid) {
				uni.navigateTo({
					url: './detailsMessage/detailsMessage?itemid=' + itemid
				});
			},
			
			// 去留言：
			publishMessage() {
				uni.navigateTo({
					url:'./publishMessage/publishMessage'
				})
			},
			
			// 获取留言列表：
			getlist() {
				uni.request({
					url: 'http://localhost:8091/freshmenapp/message/list',
					method: 'POST',
					data: {},
					success: res => {
						// console.log(res.data.list);
						if (res.data.success === true) {
							this.message = res.data.list
						}
					},
					fail: () => {},
					complete: () => {}
				});
			},

			loadmore() {
				// 上拉拼接留言列表：
				uni.request({
					url: 'http://localhost:8091/freshmenapp/message/list',
					method: 'POST',
					data: {},
					success: res => {
						// console.log(res.data.list);
						if (res.data.success === true) {
							this.message = res.data.list.concat(res.data.list)
						}
					},
					fail: () => {},
					complete: () => {
						uni.hideLoading()
					}
				});

			}
		},
		onLoad() {
			this.getlist() // 获取留言列表
		},

		// 下拉刷新：
		onPullDownRefresh() {
			this.getlist()
			
			setTimeout(function() {			
				uni.stopPullDownRefresh();
				uni.showToast({
					title: '刷新成功'
				});
			}, 2000);
			
		},

		// 上拉加载：滚动页面到距离底部50px时
		onReachBottom() {
			uni.showLoading({
				title: '加载中'
			})
			setTimeout(() => {
				this.loadmore()
				uni.showLoading({
					title: '加载成功'
				})
			}, 1000);
		},
	}
</script>

<style>
	button {
		width: 90%;
		font-size: 15px;
	}
	.btn{
		margin-top: 580px;
		margin-left: 120px;
		position: fixed;
		width: 150px;
		top: 0;
	}
</style>